<?php
require_once('./define/define_text.php');

require_once('./class/Equipe.php');
require_once('./class/Adherent.php');
require_once('./class/Evenement.php');

switch(@$_GET['action']) {
    default:
        echo _EQUIPE_;
    break;

    case "addForm":
        if(isset($_GET['id_evenement']))
        echo Equipe::getAddForm($_GET['id_evenement']);
        else echo Equipe::getAddForm();
        break;

    case "doAdd":
        $e = new Equipe();
        $e->setIdIndividu($_POST['id_individu']);
        $e->setIdEvenement($_POST['id_evenement']);
        $e->addOrModify();
        echo _MEMBER_OF_TEAM_.' '._ADDED_;
        break;

    case "list":
        $id_evenement = (isset($_GET['id'])) ? $_GET['id'] : null ;
        ($id_evenement != null) ? $equipe_array = Equipe::getEquipe($_GET['id']) : $equipe_array = Equipe::getAllEquipe();
        $html = '<table class="tablesorter">
		<thead>';

        if($id_evenement == null){
            $html .= '<th>'._ID_EVENEMENT_.'</th>';
            $html .= '<th>'._NOM_EVENEMENT_.'</th>';
        }

        $html .= '<th>'._ID_INDIVIDU_.'</th>
		<th>'._NOM_INDIVIDU_.'</th>
		<th>'._PRENOM_INDIVIDU_.'</th>
		<th>'._DELETE_.'</th>
		</thead><tbody>';
        $n = 0;
        foreach($equipe_array as $e){
            $ev = Evenement::getEvenement($e->getIdEvenement());
            $i = Adherent::getAdherent($e->getIdIndividu());
            $odd = ($n % 2 == 0) ? 'class="odd"' : "";
            $n++;
            $html .= '<tr '.$odd.'>';
            if($id_evenement == null) {
                $html .= '<td>'.$e->getIdEvenement().'</td>
					<td>'.$ev->getNom().'</td>';
            }

            $html .= '<td>'.$e->getIdIndividu().'</td>
				<td>'.$i->getNom().'</td>
				<td>'.$i->getPrenom().'</td>
      	<td><a href="?module=equipe&action=delete&id_individu='.$e->getIdIndividu().'&id_evenement='.$e->getIdEvenement().'">'._DELETE_.'</a></td>
        </tr>';
        }
        $html .= '</tbody></table>';
        if($id_evenement != null) {
            $html .= '<a href="?module=equipe&action=addForm&id_evenement='.$id_evenement.'">'._ADD_.'</a></br>';
            $html .= '<a href="?module=equipe&action=delete&id_evenement='.$id_evenement.'">'._DELETE_ALL_.'</a>';
        }
        echo $html;
        break;


    case "delete":
        if(!isset($_GET['id_individu'])) {
            $e = new Equipe();
            $e->setIdEvenement($_GET['id_evenement']);
            $e->deleteAll();
            echo _TEAM_." "._DELETED_;
        }
        else {
            $e = Equipe::getEquipe($_GET['id_evenement'], $_GET['id_individu']);
            $e->delete();
            echo _MEMBER_OF_TEAM_.' '._DELETED_;
        }
        break;
}
?>
